package com.xzakota.minesweeper.util;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * 图像工具类
 *
 * @author Xzakota
 */
public class ImageUtils {

    public static ImageIcon combineImageIcon(ImageIcon bottomIcon, ImageIcon topIcon) {
        return combineImageIcon(bottomIcon, topIcon, 1);
    }

    /**
     * 组合两个图标对象
     *
     * @param bottomIcon 将置于底层的图标
     * @param topIcon    将置于顶层的图标
     * @param scale      顶层图标缩放倍数
     * @return 组合的新图标
     */
    public static ImageIcon combineImageIcon(ImageIcon bottomIcon, ImageIcon topIcon, double scale) {
        int baseWidth = bottomIcon.getIconWidth();
        int baseHeight = bottomIcon.getIconHeight();

        int topWidth = (int) (topIcon.getIconWidth() * scale);
        int topHeight = (int) (topIcon.getIconHeight() * scale);

        BufferedImage combined = new BufferedImage(baseWidth, baseHeight, BufferedImage.TYPE_INT_ARGB);

        Graphics2D g2d = combined.createGraphics();

        g2d.drawImage(bottomIcon.getImage(), 0, 0, null);

        int x = (baseWidth - topWidth) / 2;
        int y = (baseHeight - topHeight) / 2;
        g2d.drawImage(topIcon.getImage().getScaledInstance(topWidth, topHeight, Image.SCALE_SMOOTH), x, y, null);
        g2d.dispose();

        return new ImageIcon(combined);
    }

    /**
     * 更改图标的大小
     *
     * @param icon   原图标
     * @param width  宽度
     * @param height 高度
     * @return 新图标
     */
    public static ImageIcon changeIcon(ImageIcon icon, int width, int height) {
        return new ImageIcon(icon.getImage().getScaledInstance(width, height, Image.SCALE_DEFAULT));
    }
}
